// 存储键名枚举
export enum StorageKey {
  AUTH_TOKEN = 'auth_token',
  USER_INFO = 'user_info',
  IS_AUTHENTICATED = 'is_authenticated'
}

// 存储服务类
export class StorageService {
  // 使用 chrome.storage.local 存储数据
  private static storage = chrome.storage.local

  // 存储 token
  static async setToken(token: string): Promise<void> {
    await this.storage.set({ [StorageKey.AUTH_TOKEN]: token })
  }

  // 获取 token
  static async getToken(): Promise<string | null> {
    const result = await this.storage.get(StorageKey.AUTH_TOKEN)
    return result[StorageKey.AUTH_TOKEN] || null
  }

  // 存储用户信息
  static async setUserInfo(userInfo: any): Promise<void> {
    await this.storage.set({ [StorageKey.USER_INFO]: userInfo })
  }

  // 获取用户信息
  static async getUserInfo(): Promise<any | null> {
    const result = await this.storage.get(StorageKey.USER_INFO)
    return result[StorageKey.USER_INFO] || null
  }

  // 设置认证状态
  static async setAuthenticated(isAuthenticated: boolean): Promise<void> {
    await this.storage.set({ [StorageKey.IS_AUTHENTICATED]: isAuthenticated })
  }

  // 获取认证状态
  static async getAuthenticated(): Promise<boolean> {
    const result = await this.storage.get(StorageKey.IS_AUTHENTICATED)
    return result[StorageKey.IS_AUTHENTICATED] || false
  }

  // 清除所有存储的数据
  static async clearAll(): Promise<void> {
    await this.storage.clear()
  }

  // 监听存储变化
  static addChangeListener(callback: (changes: { [key: string]: chrome.storage.StorageChange }) => void): void {
    chrome.storage.onChanged.addListener(callback)
  }

  // 移除存储变化监听
  static removeChangeListener(callback: (changes: { [key: string]: chrome.storage.StorageChange }) => void): void {
    chrome.storage.onChanged.removeListener(callback)
  }
} 